package com.mygdx.game.desktop.demo.coldDown;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.Pool;
import com.badlogic.gdx.utils.Pool.Poolable;
import com.badlogic.gdx.utils.Pools;

/** <pre>
 * 二次封装的actor
 * 
 * date: 2014-12-11
 * </pre>
 * 
 * @author caohao */
public class CHActor extends Actor implements Poolable {
	private int tag;

	private Texture bgTexture;

	private TextureRegion bgTextureRegion;

	public CHActor () {
	}

	@Override
	public void draw (Batch batch, float parentAlpha) {
		Color color = getColor();
		batch.setColor(color.r, color.g, color.b, color.a);
		//batch.setColor(1, 1, 1, 1);
		float x = getX();
		float y = getY();
		float scaleX = getScaleX();
		float scaleY = getScaleY();

		float width = getWidth();
		float height = getHeight();

		if (bgTexture != null) {
			batch.draw(bgTexture, x, y, getOriginX(), getOriginY(), getWidth(), getHeight(), scaleX, scaleY, getRotation(), 0, 0,
				(int)width, (int)height, false, false);
		}

		if (bgTextureRegion != null) {
			if (bgTextureRegion instanceof Sprite) {
				Sprite sprite = (Sprite)bgTextureRegion;
				sprite.setColor(batch.getColor());
				sprite.setOrigin(getOriginX(), getOriginY());
				sprite.setPosition(x, y);
				sprite.setScale(scaleX, scaleY);
				sprite.setSize(width, height);
				sprite.setRotation(getRotation());
				sprite.draw(batch);
			} else {
				batch.draw(bgTextureRegion, x, y, getOriginX(), getOriginY(), width, height, scaleX, scaleY, getRotation());
			}
		}
	}

	public void setBgTexture (Texture bgTexture) {
		this.bgTexture = bgTexture;
		if (bgTexture != null) {
			setSize(bgTexture.getWidth(), bgTexture.getHeight());
		}
		setOrigin(Align.center);
	}

	/** <pre>
	 * 使用缓存池
	 * 
	 * date: 2015-1-3
	 * </pre>
	 * 
	 * @author caohao
	 * @return */
	@SuppressWarnings("unchecked")
	public static <T extends CHActor> T obtain (Class<T> type) {
		Pool<CHActor> pool = (Pool<CHActor>)Pools.get(type);
		CHActor actor = pool.obtain();
		actor.setBgTexture(null);
		return (T)actor;
	}

	public static CHActor obtain () {
		return obtain(CHActor.class);
	}

	@Override
	public void reset () {
		this.bgTexture = null;
		this.bgTextureRegion = null;
		setScale(1);
		setRotation(0);
		clear();
		setUserObject(null);
		this.setColor(new Color(1, 1, 1, 1));
		setStage(null);
		setParent(null);
		setVisible(true);
		setName(null);
		setOrigin(Align.center);
		setPosition(0, 0);
	}

	public Texture getBgTexture () {
		return bgTexture;
	}

	public TextureRegion getBgTextureRegion () {
		return bgTextureRegion;
	}

	public void setBgTextureRegion (TextureRegion textureRegion) {
		this.bgTextureRegion = textureRegion;
		if (bgTextureRegion != null) {
			if (bgTextureRegion instanceof Sprite) {
				Sprite sprite = (Sprite)bgTextureRegion;
				setSize(sprite.getWidth(), sprite.getHeight());
			} else if (bgTextureRegion instanceof AtlasRegion) {
				AtlasRegion atlasRegion = (AtlasRegion)bgTextureRegion;
				//bgTextureRegion = Chao.plistCenter.createSprite(atlasRegion);
				Sprite sprite = (Sprite)bgTextureRegion;
				setSize(sprite.getWidth(), sprite.getHeight());
			} else {
				setSize(bgTextureRegion.getRegionWidth(), bgTextureRegion.getRegionHeight());
			}
		}

		setOrigin(Align.center);
	}

	@Override
	public boolean remove () {
		boolean remove = super.remove();
		if (remove) {
			Pools.free(this);
		}
		return remove;
	}

	public int getTag () {
		return tag;
	}

	public void setTag (int tag) {
		this.tag = tag;
	}

}